<?php
    //Start de sessie
    session_start();

    //Vieze hack omdat de login anders niet geintegreerd kan worden in de layout ivm 'headers already sent'-error
    ob_start();

    //Controleer of de sessie bestaat
    if(!empty($_SESSION['klantid']))
    {
    	//Ja, dan ga je door naar de index want dubbel inloggen is lame!
    	header('Location: index.php');
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Log In</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <link type="text/css" href="css/pagination.css" rel="stylesheet" />
  <link rel="stylesheet" href="css/reset.css" type="text/css" />
  <link rel="stylesheet" href="css/960.css" type="text/css" />
  <link rel="stylesheet" href="css/text.css" type="text/css" />
  <link type="text/css" href="css/tablesorter.css" rel="stylesheet" />
  <link type="text/css" href="css/sunny/jquery-ui-1.8.custom.css" rel="stylesheet" />
  <link type="text/css" href="css/jquery.autocomplete.css" rel="stylesheet" />
  <link type="text/css" href="css/style.css" rel="stylesheet" />
  <link type="text/css" href="css/accmenu.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js">
</script>
<script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js">
</script>
<script type="text/javascript" src="js/jquery.tablesorter.js">
</script>
<script type="text/javascript" src="js/jquery.autocomplete.min.js">
</script>
<script type="text/javascript" src="js/menu.js">
</script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function()
    {
        $("#producttable").tablesorter();
        $("#searchtext").autocomplete("autocomplete.php");
        $("#searchtext").result(function(event, data, formatted) {
			$("#searchform").trigger('submit');
	    });

        $("#loginbox").dialog({
			autoOpen: false,
			height: 230,
			width: 350,
			modal: true,
            buttons: {
				'Login': logincall,
				Cancel: function() {
					$(this).dialog('close');
				}
                }
		});

        $("#login button").button({
            icons: {
                primary: 'ui-icon-locked'
            }
        })
        .click(function() {
				$('#loginbox').dialog('open');
			});
        $("#register button").button();
        $("#logout button").button();
        $("#shoppingcart button").button();
    }
);

function logincall() {
      $.ajax({
      type: "POST",
      url: "login.php",
      data: $("#loginform").serialize(),
      dataType: "text",
      complete: function() {
        $(this).dialog('close');
        $(location).attr('href', "login.php");
      }
    });
  return;
}

//]]>
</script>
</head>

<body>
  <div class="container_12">
    <div id="header" class="grid_12">
      <div class="grid_5 prefix_7 alpha omega">
        <form id="searchform" name="searchform" method="get" action="search.php">
          <input type="text" name="q" id="searchtext" /> <button type="submit" id=
          "searchbutton"></button>
        </form>

        <div id="demo">
          <ul>
            <?php if(!empty($_SESSION['klantid'])){?>
            <li id="logout"><a href="logout.php"><button>Log Out</button></a></li>
            <li id="shoppingcart"><a href="view_cart.php"><button>Winkelwagen</button></a></li>
            <?php } else {?>
            <li id="login"><button>Log In</button></li>
            <li id="register"><a href="register.php"><button>Registreer</button></a></li>
            <?php } ?>
          </ul>


        </div>
        <?php if(!empty($_SESSION['klantid'])){?>
                        <p id="klantwelkom">Welkom <b><a href="account.php"><?php echo $_SESSION['fullname']; ?></a></b>&nbsp;&nbsp;
                        </p><?php } else { ?>
                        <p id="klantwelkom"><a href="forgot_pass.php">Wachtwoord vergeten?</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
                        <?php } ?>
      </div>

      <div class="clear"></div>
    </div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>

    <div id="menu" class="grid_2">

          <?php
          include_once 'temp/menu_filler.php';
          ?>


    </div>

    <div id="mainarea" class="grid_7">
        <div id="tableholder">

                <?php

                // Connects to your Database
                include 'includes/connect.php';

                // Als er op submit is gedrukt
                	if($_POST)
                	{
                		$email = $_POST['email'];
                		$wachtwoord = sha1($_POST['wachtwoord']);

                		//Voert de query uit
                		$query  = mysql_query("SELECT klantid, is_admin, voornaam, tussenvoegsel, achternaam, postcode, huisnummer, geslacht, telefoonnr, geboortedatum, email FROM klant WHERE email='$email' AND password='$wachtwoord' LIMIT 1") or die("Error: ".mysql_error());
                		//Stopt de gegevens in een array die uit de DB kwamen.
                		$result = mysql_fetch_array($query);

                		//Controleert of de user bestaat
                		if(!empty($result['klantid']))
                		{
                			//Zo ja, log hem in, en verwijs hem door naar de index pagina
                			$_SESSION['klantid'] = $result['klantid'];
                            $_SESSION['voornaam'] = $result['voornaam'];
                            $_SESSION['tussenvoegsel'] = $result['tussenvoegsel'];
                            $_SESSION['achternaam'] = $result['achternaam'];
                            $_SESSION['postcode'] = $result['postcode'];
                            $_SESSION['huisnummer'] = $result['huisnummer'];
                            $_SESSION['geslacht'] = $result['geslacht'];
                            $_SESSION['telefoonnr'] = $result['telefoonnr'];
                            $_SESSION['geboortedatum'] = $result['geboortedatum'];
                            $_SESSION['email'] = $result['email'];
                            $_SESSION['fullname'] = $result['voornaam']." ".$result['tussenvoegsel']." ".$result['achternaam'];

                            if($result['is_admin'] == 1)
                            {
		                            $_SESSION['admin_check'] = '1';
                            }
                            else
                            {
                                    $_SESSION['admin_check'] = '0';
                            }

                			header("Location: index.php");
                		}
                        else
                		{
                			//Zo nee, blijf op dezelfde pagina en toon een error.
                			$errormsg = "Het emailadres of wachtwoord is niet correct.";
                		}
                	}
                ?>

                <b><?php if(!empty($errormsg)) echo $errormsg; ?></b>
                <div id="loginbox">
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <label for="email">Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                <input type="text" value="" name="email" /><br />
                <label for="wachtwoord">Wachtwoord:</label>
                <input type="password" value="" name="wachtwoord" /><br />
                <input type="submit" value="login" name="submit" />
                </form>
                </div>

        </div>
    </div>

    <div id="sidebar" class="grid_3"><div id="tableholder"><?php include 'cart.php'; ?></div></div>

    <div class="clear"><hr />
    <b><a href="admin/admin.php">Administration panel</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="admin/adminajax2.php">Administration
    panel(ajax versie)</a></b>
    <hr /></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>
</div>
<div id="loginbox">
                <form action="#" method="POST" id="loginform">
                <br /><p><label for="email">Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                <input type="text" value="" name="email" class="text ui-widget-content ui-corner-all" /></p>
                <p><label for="wachtwoord">Wachtwoord:</label>
                <input type="password" value="" name="wachtwoord" class="text ui-widget-content ui-corner-all" /></p>
</form>
</div>
<center>
<!-- Begin Motigo Webstats counter code -->
<a id="mws4736564" href="http://webstats.motigo.com/">
<img width="80" height="15" border="0" alt="Free counter and web stats" src="http://m1.webstats.motigo.com/n80x15.gif?id=AEhGNAHBHVZi00ZSyCakWn8n38NA" /></a>
<script src="http://m1.webstats.motigo.com/c.js?id=4736564&amp;lang=NL&amp;i=3" type="text/javascript"></script>
<!-- End Motigo Webstats counter code -->
</center>
<br />
</body>
</html>
